<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use App\Models\Config;

/**
 * 后台首页控制器
 */
class LoginController extends BaseController
{


    public function __construct(Admin $admin, Config $config)
    {
        parent::__construct();
        $this->admin = $admin;
        $this->config = $config;
    }

    public function getIndex()
    {
        if (is_login()) {
            return redirect(urlAdmin('index', 'getIndex'));
        }
        return view('admin.login');
    }

    /**
     * 后台用户登录
     */
    public function postIndex()
    {
        // 检测验证码
        if (!check_verify(request('verify'))) {
            $this->error('验证码输入错误！');
        }
        $res = $this->admin->login();
        if (!$res) {
            return $this->error($this->admin->getError());
        }
        return $this->success('登录成功！', urlAdmin('index', 'getIndex'));
    }


    /* 退出登录 */
    public function getLogout()
    {
        if (is_login()) {
            session()->flush();
            return $this->success('退出成功！', urlAdmin('login', 'getIndex'));
        } else {
            return redirect(urlAdmin('login', 'getIndex'));
        }
    }
}
